package com.yinhai.message.manage.monitorDwr.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import org.directwebremoting.ScriptSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.message.implement.dwr.listener.DWRScriptSessionListener;
import com.yinhai.message.manage.monitorDwr.vo.ScriptSessionInfoVo;

/**
 * dwr scriptsession 监听查看
 * @author yanglq
 * 2017.6.11
 */
@RequestMapping("dwrMonitor")
@Controller
public class DwrMonitorController extends BaseController{
	
	/**
	 * 页面初始化
	 * @return
	 */
	@RequestMapping("dwrMonitorController.do")
	public String execute(){
		setList("dwrGrid", getScriprtSessionInfo());
		return "/manage/monitordwr/dwrMonitor";
	}
	
	/**
	 * 刷新
	 * @return
	 */
	@RequestMapping("dwrMonitorController!show.do")
	public String toShow(){
		setList("dwrGrid", getScriprtSessionInfo());
		return JSON;
	}
	
	private List<ScriptSessionInfoVo> getScriprtSessionInfo(){
		List<ScriptSessionInfoVo> list = new ArrayList<>();
		for(Entry<String, ScriptSession> entry : DWRScriptSessionListener.scriptSessionMap.entrySet()){
			ScriptSessionInfoVo vo = new ScriptSessionInfoVo();
			ScriptSession sc = entry.getValue();
			vo.setId(entry.getKey());
			vo.setScriptSessionId(sc.getId());
			vo.setHttpSessionId(sc.getHttpSessionId());
			vo.setEffective(sc.isInvalidated());
			vo.setCreationTime(new Date(sc.getCreationTime()));
			vo.setLastAccessedTime(new Date(sc.getLastAccessedTime()));
			vo.setSystemId((String) sc.getAttribute("systemid"));
			vo.setUserId((String) sc.getAttribute("userid"));
			vo.setPageId((String) sc.getAttribute("pageid"));
			list.add(vo);
		}
		return list;
	}
}
